package com.gfdd.tea.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gfdd.tea.mapper.UserMapper;
import com.gfdd.tea.pojo.AddressBook;
import com.gfdd.tea.pojo.User;
import com.gfdd.tea.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("userService")
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Override
    public User selectById(String openId) {
        User user = (User) userMapper.selUserById(openId);
        return user;
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insUser(user);
    }

    @Override
    public int updateUser(User user) {
        int i = userMapper.updByUID(user);
        return i;
    }

    @Override
    public AddressBook selectAddress(int user_id) {
        return (AddressBook) userMapper.selAddress(user_id);
    }

    @Override
    public int insetAddress(AddressBook addressBook) {
        return userMapper.insAddress(addressBook);
    }

    @Override
    public int updateAddress(AddressBook addressBook) {
        if (addressBook.getIsDefault() == 1) {
            int i = userMapper.updAddressIsDefaultToZero(addressBook.getUserId());
            if (i > 0) {
                return userMapper.updAddress(addressBook);
            } else {
                return 0;
            }
        } else {
            return userMapper.updAddress(addressBook);
        }
    }

    @Override
    public int deleteAddress(int id) {
        return userMapper.delAddress(id);
    }

}
